<template>
	<div>
		<Message
			v-if="errorMsg"
			variant="danger"
			class="mb-4"
		>
			{{ errorMsg }}
		</Message>
		<div
			v-if="isSuccess"
			class="has-text-centered mb-4"
		>
			<Message variant="success">
				{{ $t('user.auth.resetPasswordSuccess') }}
			</Message>
			<x-button
				:to="{ name: 'user.login' }"
				class="mt-4"
			>
				{{ $t('user.auth.login') }}
			</x-button>
		</div>
		<form
			v-if="!isSuccess"
			@submit.prevent="requestPasswordReset"
		>
			<div class="field">
				<label
					class="label"
					for="email"
				>{{ $t('user.auth.email') }}</label>
				<div class="control">
					<input
						id="email"
						v-model="passwordReset.email"
						v-focus
						class="input"
						name="email"
						:placeholder="$t('user.auth.emailPlaceholder')"
						required
						type="email"
					>
				</div>
			</div>

			<div class="field is-grouped">
				<div class="control">
					<x-button
						type="submit"
						:loading="passwordResetService.loading"
					>
						{{ $t('user.auth.resetPasswordAction') }}
					</x-button>
					<x-button
						:to="{ name: 'user.login' }"
						variant="secondary"
					>
						{{ $t('user.auth.login') }}
					</x-button>
				</div>
			</div>
		</form>
	</div>
</template>

<script setup lang="ts">
import {ref, shallowReactive} from 'vue'

import PasswordResetModel from '@/models/passwordReset'
import PasswordResetService from '@/services/passwordReset'
import Message from '@/components/misc/Message.vue'

const passwordResetService = shallowReactive(new PasswordResetService())
const passwordReset = ref(new PasswordResetModel())
const errorMsg = ref('')
const isSuccess = ref(false)

async function requestPasswordReset() {
	errorMsg.value = ''
	try {
		await passwordResetService.requestResetPassword(passwordReset.value)
		isSuccess.value = true
	} catch (e) {
		errorMsg.value = e.response.data.message
	}
}
</script>

<style scoped>
.button {
	margin: 0 0.4rem 0 0;
}
</style>
